classdef zoo_cl
    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        server = 'localhost:9998';
        protocol = 'http://';      
        rc;
    end
    
    methods
        function obj=zoo_cl
            addpath(genpath('parse_json'));
            dpath = javaclasspath;
            if isempty(dpath) || isempty(strfind(dpath{1},'zoo'))
                javaaddpath('zooConnector/bin');        
            end
            import rest_cl.*;            
            obj.rc=rest_cl.RestClient();
            % rc=RestClient();
        end
        
        %get children of the root node
        % zoo_cl().get_children_of('/')
        function res=get_children_of(obj,nn)        
                % s = urlread(sprintf('http://localhost:9998/znodes/v1/%s?view=children',nn));
                s=obj.rc.executeGet(sprintf('http://localhost:9998/znodes/v1%s?view=children',nn));
              if not(isempty(s))
                    str=char(s);
                    data = parse_json(str);
                    res=data.children(:); 
              else
                    err = MException('RestCl:NodeNotExists','node does not exists');
                    throw(err);
              end
        end

        %get node (e.g. "/cluster1/leader") as json 
        %  zoo_cl().get_node_data('/')
        function res=get_node_data(obj,nn)       
            % s = urlread(sprintf('http://localhost:9998/znodes/v1%s',nn)); 
            s=obj.rc.executeGet(sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn));           
            if not(isempty(s))
                 str=char(s);
                data = parse_json(str); % char converts java.lang.String to matlab string
            else
                err = MException('RestCl:NodeNotExists','node does not exists');
                throw(err);
            end
            res=data.dataUtf8;
        end     
        
%   HTTP's GET, PUT, HEAD, and DELETE operations map naturally to
%   ZooKeeper's "get," "set," "exists," and "delete" operations.
        function res=if_node_exists(obj,nn)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'HEAD',''); 
               res=true;
            catch exception
               % disp(exception.identifier);
               res=false;
            end                        
        end

        function res=delete_node(obj,nn)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'DELETE',''); 
               wentThrough=true;
            catch exception
               % disp(exception.identifier);
               wentThrough=false;
            end                        
        end
        
        %set a node (data.txt contains the ascii text you want to set on the node)
        % zoo_cl().set_node('/dude','bye')
        function res=set_node(obj,nn,content)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'PUT',content); 
               wentThrough=true;
            catch exception
               % disp(exception.identifier);
               wentThrough=false;
            end                        
        end

        %create a node
        % zoo_cl().create_node('/test', 'hi')     
        function wentThrough=create_node(obj,node_full_name,data,is_sequential,is_ephemeral)                                 
            [tok mat] =regexpi(node_full_name, '/(.*/)?(\w+)?', 'tokens','match');
            %            if (~(isempty(tok)))               
            node_path = tok{1}{1};
            if not(isempty(node_path))
                node_path=node_path(1:end-1); 
            end
            nn=tok{1}{2};
            % {node_path , nn}
            url=sprintf('http://localhost:9998/znodes/v1/%s?op=create&name=%s',...
                node_path,nn); 
            if (is_sequential==true)
                url=strcat(url, '&sequence=true');
            end
            if (is_ephemeral==true)
                 url=strcat(url,  '&ephemeral=true'); 
            end
           
          %   url='http://localhost:9998/znodes/v1/cluster9?op=create&name=huj&dataformat=utf8'; 
            wentThrough =  obj.rc.executePost(url,data);  
        end
        
%         %create a new session 
%         function data=test(obj)     
%             s = urlread( -d "" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/?op=create&expire=10"
%         end
        function res=if_node_exists(obj,nn)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'HEAD',''); 
               res=true;
            catch exception
               % disp(exception.identifier);
               res=false;
            end                        
        end
        
% 
%         %session heartbeat
%         function data=test(obj)          
%             s = urlread( -X "PUT" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/02dfdcc8-8667-4e53-a6f8-ca5c2b495a72"
%         end
        function res=if_node_exists(obj,nn)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'HEAD',''); 
               res=true;
            catch exception
               % disp(exception.identifier);
               res=false;
            end                        
        end 
        
% 
%         %delete a session
%         function data=test(obj)   
%             s = urlread( -X "DELETE" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/02dfdcc8-8667-4e53-a6f8-ca5c2b495a72"
%         end
        function res=if_node_exists(obj,nn)                      
            urlStr = sprintf('http://localhost:9998/znodes/v1%s?dataformat=utf8',nn);
            res='';
            try
               res =  obj.rc.execute(urlStr,'HEAD',''); 
               res=true;
            catch exception
               % disp(exception.identifier);
               res=false;
            end                        
        end

           function test(obj, name)                             
%                obj.create_node(name, 'hi'); 
%                obj.get_node_data(name)
               obj.set_node(name,'bye')
%                obj.get_node_data(name)
%                obj.get_children_of(name)
%                obj.create_node(sprintf('%s/child',name), 'hi')
%                obj.get_children_of(name)               
           end
    end
    
end

